package com.zero.clinic.service.impl;


import com.zero.clinic.mapper.RegisterMapper;
import com.zero.clinic.mapper.RegisterSubMapper;
import com.zero.clinic.pojo.Register;
import com.zero.clinic.pojo.RegisterExample;
import com.zero.clinic.pojo.RegisterSub;
import com.zero.clinic.pojo.RegisterSubExample;
import com.zero.clinic.service.RegisterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Resource
    private RegisterMapper registerMapper;

    @Resource
    private RegisterSubMapper registerSubMapper;

    @Override
    public List<Register> selectByExample(RegisterExample example) {
        return registerMapper.selectByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Long registerId) {
        //1.通过registerId删除挂号费表中的数据
        int registerDeleteReuslt = registerMapper.deleteByPrimaryKey(registerId);
        //2.通过registerId删除挂号费子费用表中对应的数据
        RegisterSubExample example = new RegisterSubExample();
        RegisterSubExample.Criteria criteria = example.createCriteria();
        criteria.andRegisterIdEqualTo(registerId);
        registerSubMapper.deleteByExample(example);

        return registerDeleteReuslt;
    }

    @Override
    public int insert(Register register) {
        //1.先新增挂号费信息，然后获取到从数据库返回的registerId
        System.out.println("插入数据前的主键：" + register.getRegisterId());
        int insertRegisterResult = registerMapper.insert(register);
        System.out.println("插入数据后的主键：" + register.getRegisterId());
        //2.遍历registerId中所有的RegisterSub对象，将registerId赋值给每个RegisterSub后插入数据库
        Long registerId = register.getRegisterId();
        for (RegisterSub registerSub : register.getRegisterSubs()) {
            registerSub.setRegisterId(registerId);
            registerSubMapper.insert(registerSub);
        }

        return insertRegisterResult;
    }

    @Override
    public int insertSelective(Register record) {
        return registerMapper.insertSelective(record);
    }

    @Override
    public Register selectByPrimaryKey(Long registerId) {
        // 1.先通过registerId查出对应的register对象
        Register register = registerMapper.selectByPrimaryKey(registerId);
        // 2.再通过registerId查出对应的List<registerSub>
        RegisterSubExample example = new RegisterSubExample();
        RegisterSubExample.Criteria criteria = example.createCriteria();
        criteria.andRegisterIdEqualTo(registerId);
        List<RegisterSub> registerSubs = registerSubMapper.selectByExample(example);
        // 3.把查到的registerSubs赋值给register对象后返回给方法
        register.setRegisterSubs(registerSubs);
        return register;
    }

    @Override
    public int updateByPrimaryKeySelective(Register register) {
        // 1.先更新挂号费信息
        int updateRegisterResult = registerMapper.updateByPrimaryKeySelective(register);
        // 2.根据前端传过来的register获取到对应的registerSubs
        List<RegisterSub> registerSubs = register.getRegisterSubs();
        // 3.遍历registerSubs，然后将每一个registerSub插入到数据库中
        for (RegisterSub registerSub : registerSubs) {
            registerSubMapper.updateByPrimaryKeySelective(registerSub);
        }

        return updateRegisterResult;
    }

    @Override
    public int deleteByIds(String[] registerIds) {
        //1.通过registerIds删除挂号费表中的数据
        int registersDeleteResult = registerMapper.deleteByIds(registerIds);
        //2.通过registerIds删除挂号费子费用表中对应的数据
        for (String registerId : registerIds) {
            RegisterSubExample example = new RegisterSubExample();
            RegisterSubExample.Criteria criteria = example.createCriteria();
            criteria.andRegisterIdEqualTo(Long.parseLong(registerId));
            registerSubMapper.deleteByExample(example);
        }

        return registersDeleteResult;
    }

    @Override
    public List<RegisterSub> getRegisterSubsByRegisterId(Long registerId) {
        RegisterSubExample example = new RegisterSubExample();
        RegisterSubExample.Criteria criteria = example.createCriteria();
        criteria.andRegisterIdEqualTo(registerId);
        List<RegisterSub> registerSubs = registerSubMapper.selectByExample(example);
        return registerSubs;
    }
}
